home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 29 Feb 96 14:47:25 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.825605245@rscernix>
- References: <4gqpa1$3h9@alcor.usc.edu> <4gsdno$1bg@umbc9.umbc.edu> <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com> <4h1u9d$sqq@ceylon.gte.com>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4h1u9d$sqq@ceylon.gte.com> Brenda <g051286> writes:
-
- >What is the definition of a pointer? I have always been taught that a
- >pointer is simply an address in memory, and an array name (i.e. myarray)
- >is simply a CONSTANT address.
-
- Your teacher obviously didn't know what s/he was talking about.
- Or you grossly misunderstood what s/he taught you. And you definitely
- didn't bother reading the FAQ before posting.
-
- > I don't think this statement is all that
- >radical. Of course there are differences between arrays and pointers
- >due to the fact that an array is a CONSTANT address and a pointer is a
- >VARIABLE address.
-
- There are also differences caused by the fact that arrays are NOT pointers.
- For the benefit of people who need to see to be able to believe, here's
- a simple example:
-
- cnwgs1:~/tmp 13> cat dumb.c
- #include <stdio.h>
- char array[] = "abcd";
-
- foo()
- {
- printf("dumb.c\narray is stored at address: %p\n", (void *)array);
- printf("array contains: %s\n", array);
- }
-
- cnwgs1:~/tmp 14> cat dumber.c
- #include <stdio.h>
- extern char *array; /* arrays are pointers, right? :-) */
-
- main()
- {
- foo();
- puts("dumber.c");
- printf("array is pointing at the address: %p\n", (void *)array);
- printf("%x='a' %x='b' %x='c' %x='d'\n", 'a', 'b', 'c', 'd');
- printf("array contains: %s\n", array);
- return 0; /* Not reached */
- }
-
- cnwgs1:~/tmp 15> cc dumb.c dumber.c
- dumb.c:
- dumber.c:
- cnwgs1:~/tmp 16> ./a.out
- dumb.c
- array is stored at address: 200524b8
- array contains: abcd
- dumber.c
- array is pointing at the address: 61626364
- 61='a' 62='b' 63='c' 64='d'
- Segmentation fault (core dumped)
-
- Looks like there is something wrong with the "arrays are pointers" theory.
- I've provided all the clues in the output, so that anybody can figure out
- what actually happens in this program. The FAQ explains this with a nice
- picture, but since some people cannot be convinced to read it...
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-